<?php

namespace app\common\model;

use app\common\interfaces\RevokeOrder;
use think\Model;

class OrderGoods extends Model implements RevokeOrder
{

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = '';
  
    const STATUS_NO     = 0;    //未使用
    const STATUS_YES    = 1;    //已使用
    const STATUS_EXPIRE = 2;    //过期
    const STATUS_REFUND = 3;    //退款

    /**
     * @return 凭证号
     * @param $order_id 订单号
     * @param $num 退票数量
     * 退款操作，更新订单项中单个单个商品的状态
     */
    public function revokeOrder($order_id, $num)
    {
        $list = $this->where(['order_id' => $order_id,'status' => self::STATUS_NO])->select();
        if (!$list) {
            return false;
        }
        foreach ($list as $key => $goods) {
            if ($key < $num) {
                $update[$key] = [
                    'id'          => $goods['id'],
                    'status'      => self::STATUS_REFUND
                ];
                $voucher_no[] = $goods['voucher_no'];
            }
        }
        $voucher_no = reset($voucher_no);
        $this->saveAll($update);
        return $voucher_no;
    }

    /****
     * @param \app\common\controller\订单id $order_id
     * @param \app\common\controller\退票数量 $num
     * @return bool|mixed
     * 去取撤消（退款）订单
     */
    public function recallRefund($order_id,$num)
    {
        $goods_list = $this->where(['order_id' => $order_id,'status' => self::STATUS_REFUND])->limit($num)->select();

        foreach ($goods_list as $key => $goods){
            $update[$key] = [
                'id'          => $goods['id'],
                'status'      => self::STATUS_NO,
                // 'update_time' => time(),
            ];

            //多票一码
            $voucher_no = $goods->voucher_no;

        }

        $this->saveAll($update);
        return $voucher_no;
    }

    /**
     * @param $order_id 订单id
     * @param $number 取消退款数量
     * @return bool
     * 取消退款检查票的数量
     */
    public function checkTicketNum($order_id,$number)
    {
        $total = $this->where(['order_id'=>$order_id])->value('buy_nums');//购买票的总数
        $other_states_num = $this->where(['order_id'=>$order_id,'status'=>['neq',self::STATUS_REFUND]])->count();//除退款完，其他状态的数量
        $sum = $number + $other_states_num;//取消退票数量 + 其他票状态的数量之和
        if ($sum == $total){
            //全部取消退款
            return true;
        }else{
            //存在退款
            return false;
        }
    }

    /***
     * @param $voucher_no
     * @param $consume_num
     * 验证票操作
     */
    public function checkTicket($voucher_no,$consume_num,$order_id)
    {

        $where['status']     = [['=',0],['=',2],'or'];  //未使用
        $where['voucher_no'] = $voucher_no;
        $where['order_id']   = $order_id;
        $order_goods = $this->where($where)->select();
        $order       = new Order();
        foreach ($order_goods as $key=>$goods){
            if ($key < $consume_num){
                $data[$key] = [
                    'id'          => $goods->id,
                    'status'      => self::STATUS_YES,
                    // 'update_time' => time(),
                    'check_time'  => time()
                ];
                $order_id = $goods->order_id;
            }
        }
        $this->isUpdate(true)->saveAll($data);
        $result = $this->notUsed($order_id);
        $order->changeOrderUsageStatus($order_id,$result);
    }

    /***
     * @param $voucher_no
     * @return bool
     * 门票是否使用完 true 消费完, false 未消费完
     */
    public function notUsed($order_id)
    {
        $order_goods = $this->where(['order_id' => $order_id,'status'   => self::STATUS_NO])->select();
      //  halt(collection($order_goods)->toArray());
        if ($order_goods){
            //未消费完
            return false;
        }else{
            //消费完
            return true;
        }
    }

    /***
     * @param $order_id
     * 订单项过期
     */
    public function expired($order_id)
    {
        $this->where(['order_id'=> $order_id,'status'=> self::STATUS_NO])->update(['status'=>self::STATUS_EXPIRE]);
    }

}